package com.hu.zhcc.shiro.controller.dingming;

import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.shiro.entity.dingming.TCheckitem;
import com.hu.zhcc.shiro.service.dingming.TCheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/tCheckItem")
public class TCheckItemController {
    @Autowired
    private TCheckItemService tCheckItemService;

    @PostMapping("")
    public ResponseEntity<TCheckitem> saveCheckItem(@RequestBody TCheckitem checkItem) {
        checkItem.setCreatetime(new Date());
        int insert = tCheckItemService.insert(checkItem);
        return ResponseEntity.status(HttpStatus.CREATED).body(checkItem);
    }

    @GetMapping("/{id}")
    public ResponseEntity<TCheckitem> getCheckItemById(@PathVariable("id") Integer id) {
        TCheckitem checkItem = tCheckItemService.selectByPrimaryKey(id);
        if (checkItem != null) {
            return ResponseEntity.ok(checkItem);
        }
        return ResponseEntity.notFound().build();
    }

    @PutMapping("")
    public ResponseEntity<Integer> updateCheckItem(@RequestBody TCheckitem checkItem) {
        int rows = tCheckItemService.updateByPrimaryKey(checkItem);
        if (rows == 0) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.status(HttpStatus.CREATED).body(rows);
    }

    @GetMapping("")
    public ResponseEntity<Page<TCheckitem>> getCheckItemList(@RequestParam(required = false) String name,int offset, int limit) {
        Map<String, Object> paremeters = new HashMap<String, Object>();
        paremeters.put("name", name);
        Page<TCheckitem> result = tCheckItemService.listCheckItem(paremeters, offset, limit);
        return ResponseEntity.ok(result);
    }
}
